local json=require("cocos.cocos2d.json")
local FZdownloaded="FZdownloaded"
local FZcache="FZcache"
local FZactivity="activity"
local origin_info_filename="origin_info.lua"
local server_list_filename="server_list.json"
local open_config_filename="OpenConfig.lua"
local server_info_filename="server_info.lua"

local FileUtils=cc.FileUtils:getInstance()

local FzFileUtils={}

FzFileUtils.origin_info_filename=origin_info_filename
FzFileUtils.server_list_filename=server_list_filename
FzFileUtils.open_config_filename=open_config_filename
FzFileUtils.server_info_filename=server_info_filename

function FzFileUtils.getServerListPath()
    return FzFileUtils.getDownloadPath().."/"..server_list_filename
end

--用于require(origin_info),返回的是"origin_info"
function FzFileUtils.getOriginInfoPath()
    return "origin_info"
end

function FzFileUtils.getDownloadPath()
	return FileUtils:getWritablePath()..FZdownloaded
end

function FzFileUtils.getCachePath()
	return FileUtils:getWritablePath()..FZcache
end

function FzFileUtils.writeCache(dataMap,fileName)
    local cache=FzFileUtils.getCachePath()
    if not FileUtils:isDirectoryExist(cache) then
        FileUtils:createDirectory(cache)
    end
    local path=FzFileUtils.getCachePath().."/"..fileName
    cc.FileUtils:getInstance():writeToFile(dataMap,path)
end

function FzFileUtils.readCache(fileName)
    local path=FzFileUtils.getCachePath().."/"..fileName
    return cc.FileUtils:getInstance():getValueMapFromFile(path)
end

function FzFileUtils.getActivityPath()
    return FzFileUtils.getDownloadPath().."/"..FZactivity
end

function FzFileUtils.writeFile(fileName,path,buf)
    if not FileUtils:isDirectoryExist(path) then
        FileUtils:createDirectory(path)
    end
    local file_url=path.."/"..fileName
    local wfile=io.open(file_url, "wb")                   
    assert(wfile,"打开文件错误，路径："..file_url)    --打开时验证是否出错  
    wfile:write(buf)                                    
    wfile:flush()  
    wfile:close()           
end

function FzFileUtils.readFile(fileFullName)
    return FileUtils:getStringFromFile(fileFullName)
end

function FzFileUtils.isExist(filefullname)
    return FileUtils:isFileExist(filefullname)
end

function FzFileUtils.deleteDir(path)
    if FzFileUtils.isExist(path) then 
        for file in lfs.dir(path) do
            if file ~= '.' and file ~= '..' then
                local filepath = path .. '/' .. file
                local mode = lfs.attributes(filepath,"mode")
                if mode == 'directory' then
                    FzFileUtils.deleteDir(filepath)
                else
                    local removefileret=FileUtils:removeFile(filepath)
                    print("remove file ",filepath,removefileret)
                end
            end
        end
        local ret, des = os.remove(path)
        if ret then
            print("delete dir",path,"successfull")
        else
            print("delete dir",des)
        end
        return ret
    else
        return false
    end
end

--删除多余的.lua
function FzFileUtils.deleteLuaOfDir(path)
    if FzFileUtils.isExist(path) then 
        for file in lfs.dir(path) do
            if file ~= '.' and file ~= '..' then
                local filepath = path .. '/' .. file
                local mode = lfs.attributes(filepath,"mode")
                if mode == 'directory' then
                    FzFileUtils.deleteLuaOfDir(filepath)
                else
                    if string.sub(file, -4, -1) == ".lua" then
                        local removefileret=FileUtils:removeFile(filepath)
                        print("remove file ",filepath,removefileret)
                    end
                end
            end
        end
    end
end

local lua_string_split=string.split

local function checkClientVersion( download_version , local_version )
    local ret=lua_string_split(download_version,".")
    local ret2=lua_string_split(local_version,".")
    for i,v in ipairs(ret) do
        local down_=tonumber(ret[i]) or 0
        local local_=tonumber(ret2[i]) or 0
        if local_>down_ then 
            return true 
        elseif local_<down_ then 
            return false 
        end
    end
    return false
end

local fzfile="fzversion"
local function deleteDirAndWriteFzFile()
    local info=require("info")
    FzFileUtils.deleteDir(FzFileUtils.getDownloadPath())
    FzFileUtils.writeFile(fzfile,FzFileUtils.getDownloadPath(),""..info.cpp_version)
end
FzFileUtils.deleteDirAndWriteFzFile=deleteDirAndWriteFzFile

function FzFileUtils.deleteOldFiles()
    local fzpath=FzFileUtils.getDownloadPath() .. "/" .. fzfile
    if FzFileUtils.isExist(fzpath) then 
        local fzbuff=FzFileUtils.readFile(fzpath)
        local fzvalue=tonumber(fzbuff)
        local info=require("info")
        if info.cpp_version>fzvalue then 
            deleteDirAndWriteFzFile()
        else
            local path=FzFileUtils.getDownloadPath().."/project.manifest"
            local local_path="res/project.manifest"
            print("path",path)
            print("local_path",local_path)
            if FzFileUtils.isExist(path) then
                local download_json=json.decode(FzFileUtils.readFile(path))
                local download_version=download_json.version
                local local_json=json.decode(FzFileUtils.readFile(local_path))
                local local_version=local_json.version
                print("download_version",download_version)
                print("local_version",local_version)
                if checkClientVersion(download_version , local_version) then 
                    deleteDirAndWriteFzFile()
                end
            end 
        end
    else --不存在fzversion文件，删除目录
        deleteDirAndWriteFzFile()
    end
end

return FzFileUtils